package inter.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ProRentOrders;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TRentproduct;
import com.dingpeikeji.quickdeer.modules.rentproduct.service.RentproductOrderService;
import com.dingpeikeji.quickdeer.modules.utils.ValidateTool;

import inter.authorization.annotation.Authorization;
import inter.model.ResultModel;

/**
 * 整包车操作接口
 * @author zijuan
 *
 */
@Controller
@RequestMapping(value = "api")
public class RentProductOperate {
	@Autowired
	private RentproductOrderService rentproductOrderService;
	
	/**
	 * 整包车查询
	 * @return
	 */
	@RequestMapping(value = "rentProductQuery")
	public ResponseEntity<ResultModel> rentProductQuery(TRentproduct tRentproduct,String time,Integer days) {
		// ====================>>校验数据开始
		StringBuilder messages = new StringBuilder(); // 消息
		// 发车日期非空，格式yyyy-MM-dd，默认明天，最小今天
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null; // 当前年月日
		try {
			date = sdf.parse(sdf.format(new Date()));
		} catch (ParseException e) { // 这里不会抛异常的
		}
		String fromdateStr = tRentproduct.getFromdateStr();
		if (StringUtils.isEmpty(fromdateStr)) { // 如果发车日期为空
			// 默认明天
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DAY_OF_MONTH, 1);
			tRentproduct.setFromdateStr(sdf.format(calendar.getTime()));
		} else {
			try {
				// 日期最小是今天
				Date fromDate = sdf.parse(tRentproduct.getFromdateStr());
				if(fromDate.compareTo(date)<0){
					messages.append("发车日期不能小于今天  ");
				}
			} catch (ParseException e) {
				messages.append("发车日期格式非法  ");
			}
		}
		// 发车时间非空，默认上午6点
		if(StringUtils.isEmpty(time)) {
			time = "06:00";
		} else {
			try {
				new SimpleDateFormat("HH:mm").parse(time);
			} catch (ParseException e) {
				messages.append("发车时间格式非法  ");
			}
		}
		// 服务开始城市非空
		if(tRentproduct.getDepartcity()==null || tRentproduct.getDepartcity().getSiteid()==null){
			messages.append("服务开始城市非空  ");
		}
		
		// 包车天数为空，1天-10天选择，默认1天
		if(days == null){
			days = 1; // 包车天数默认为1
		}
		// ====================>>数据校验结束
		if(ValidateTool.isEmptyOrNull(messages.toString())) {//校验通过
			/*// 当前下午2点停止卖票
			Calendar cal = Calendar.getInstance();
			if(cal.get(Calendar.HOUR_OF_DAY) >= 14) {
				messages.append("抱歉！每日14：00后只能预定后天车辆，为保障您的顺利出行，请拨打平台客服电话4009287733，转人工预定。");
			}*/
			
			// 根据包车天数计算租赁结束日期
			Calendar calendar = Calendar.getInstance();
			try {
				calendar.setTime(sdf.parse(tRentproduct.getFromdateStr()));
			} catch (ParseException e) { // 日期解析不会出现问题
			}
			calendar.add(Calendar.DAY_OF_MONTH, days);
			tRentproduct.setStopdateStr(sdf.format(calendar.getTime()));
			List<TRentproduct> tRentproductList = rentproductOrderService.findTRentproductList(tRentproduct);
			return new ResponseEntity<ResultModel>(ResultModel.ok(tRentproductList), HttpStatus.OK);//返回成功json
		}else{
			return new ResponseEntity<ResultModel>(ResultModel.no(messages), HttpStatus.OK);//返回失败json
		}
		
	}
	
	
	/**
	 * 整包车预定申请
	 * @param userid
	 * @param orders
	 * @return
	 */
	@RequestMapping(value = "rentProductBooking")
	@Authorization
	public ResponseEntity<ResultModel> rentProductBooking(@RequestParam String userid,TOrders orders) {
		orders.setOrderuserid(userid);
		// ====================>>校验数据开始
		StringBuilder messages = new StringBuilder(); // 消息
		String purchasenum = orders.getPurchasenum(); // 车辆数
		String boardaddr = orders.getBoardaddr(); // 上客点
		String contacts = orders.getContacts(); // 联系人
		String payertel = orders.getPayertel(); // 联系电话
		if(ValidateTool.isEmptyOrNull(purchasenum)){
			messages.append("车辆非空");
		} else if (!ValidateTool.isNonnegativeDigit(purchasenum)) {
			messages.append("车辆非负整数");
		}
		if(ValidateTool.isEmptyOrNull(boardaddr)){
			messages.append("上客点非空");
		}
		if(ValidateTool.isEmptyOrNull(contacts)){
			messages.append("联系人非空");
		}
		if(ValidateTool.isEmptyOrNull(payertel)){
			messages.append("联系电话非空");
		} else if(!ValidateTool.isMobile(payertel)) {
			messages.append("联系电话非法");
		}
		// ====================>>校验数据结束
		if(ValidateTool.isEmptyOrNull(messages.toString())) {//校验通过
			ProRentOrders pro = null;
			try {
				pro = rentproductOrderService.genOrder(orders);
				if(pro.success()) { //订单生成成功
					Integer orderid = pro.getOrderid();
					return new ResponseEntity<ResultModel>(ResultModel.ok(), HttpStatus.OK);//返回成功json
				}
				messages.append(pro.getReturnmsg());
			} catch (Exception e) {
				messages.append(e.getMessage());
			}
		}
		return new ResponseEntity<ResultModel>(ResultModel.no(messages.toString()), HttpStatus.OK);//返回失败json
	}
	

}
